import {
	getToken
} from '@/utils/auth'

// 登录页面
const loginPage = "/pages/login"

// 页面白名单
const whiteList = [
	"/pages/index",
	"/pages/team/index",
	// "/pages/mine/index",
	
	
	'/pages/login',
	'/pages/register',
	'/pages/help',
	'/pages/record',

	'/pages/payLoading',
	'/pages/loading',

	'/pages/ordinary',
	'/pages/bd',

	'/pages/professional',
	'/pages/bdp',

	'/pages/marriage',
	'/pages/check',
	'/pages/mar',

	'/pages/judicial',
	'/pages/jud',

	'/pages/company',
	'/pages/qyns',
	'/pages/com',

	'/pages/immovables',
	'/pages/imm',
	
	'/pages/pwd',
]

// 检查地址白名单
function checkWhite(url) {
	const path = url.split('?')[0]
	return whiteList.indexOf(path) !== -1
}

// 页面跳转验证拦截器
let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"]
list.forEach(item => {
	uni.addInterceptor(item, {
		invoke(to) {
			console.log('---permission---')
			if (getToken()) {		// 已登录原地放过
				if (to.url === loginPage) {
					uni.reLaunch({
						url: "/"
					})
				}
				return true
			} else {
				
				if (checkWhite(to.url)) {
					return true
				}
				uni.reLaunch({
					url: loginPage
				})
				return false
			}
		},
		fail(err) {
			console.error(err)
		}
	})
})